package com.wanmait.baohan.controller.personnelController;

import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.NationAndJobVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class NewStaffController {
    @Resource
    private StaffService staffService;

    @Resource
    private NationService nationService;
    @Resource
    private RoleService roleService;
    @RequestMapping("personal/newcomer")
    public String newcomer(Model model){
       /*
       拿出所有的role
        */
        List<Role> roles=roleService.selectFindAll();
        model.addAttribute("roles",roles);
        return "personal/newcomer";
    }
    @RequestMapping("/personal/requestJobNumber")
    @ResponseBody
    public NationAndJobVo requestNewcomer(){
        String  count=staffService.generateJobNumber();
        List<Nation> nations=nationService.findAllnation();
        NationAndJobVo nationAndJobVo=new NationAndJobVo();
        nationAndJobVo.setJobNumber(count);
        nationAndJobVo.setNation(nations);
        //System.out.println(count);
        return nationAndJobVo;
    }
    @RequestMapping("/personal/personalInformation")
    public String personalInformation(HttpServletRequest request, Staff staff, MultipartFile Unchangedphone, MultipartFile Unchangedresume,Integer staffRoles[]){
//        System.out.println("姓名====="+staff.getName());
//        System.out.println("jonNumber:"+staff.getJobNumber());
//        System.out.println("123131321321");
//        System.out.println("头像======"+Unchangedphone.getOriginalFilename());
//        System.out.println("简历======"+Unchangedresume.getOriginalFilename());
//        System.out.println("角色的id========长度"+staffRoles.length);
        List<StaffRole> roleList=new ArrayList<>();
        for (int i = 0; i <staffRoles.length ; i++) {
            System.out.println(staffRoles[i]);
            StaffRole staffRole=new StaffRole();
            staffRole.setRoleId(staffRoles[i]);
            roleList.add(staffRole);
        }
        if((!Unchangedphone.isEmpty())&&(!Unchangedresume.isEmpty())) {
            String ext = FilenameUtils.getExtension(Unchangedphone.getOriginalFilename());
            String ext1=FilenameUtils.getExtension(Unchangedresume.getOriginalFilename());
            if ((!",jpg,png,gif,bmp,jpeg,webp,".contains("," + ext + ","))) {
                //System.out.println("进入判断======");
                return null;
            }
            try {
               // System.out.println("进入改名==============");
                //唯一文件名
                String fileName = UUID.randomUUID() + "." + ext;
                String fileName1=UUID.randomUUID()+"."+ext1;
                //物理路径

                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/image/headers";//request.getServletContext().getRealPath("/static/image");
                String path1 = ResourceUtils.getURL("classpath:").getPath()+ "/static/image/otherFiles";//request.getServletContext().getRealPath("/static/otherFiles");
                File file = new File(path, fileName);
                File file1=new File(path1,fileName1);
                Unchangedphone.transferTo(file);
                Unchangedresume.transferTo(file1);
                staff.setPhoto(fileName);
                staff.setResume(fileName1);
                //System.out.println("文件改名成功===================");
            } catch (IOException e) {
                e.printStackTrace();
            }
            HttpSession session=request.getSession();
            Staff id=(Staff)session.getAttribute("user");
            staff.setCreateById(id.getId());
            staff.setState(true);
           // System.out.println("添加人员=======");

            staffService.insertPersonnel(staff,roleList);
        }
        return "personal/personnel";
    }

}
